const columns = [
  {
    title: "点位名称",
    dataIndex: "posName",
    scopedSlots: { customRender: "poitionName" },
    align: "center"
  },
  {
    title: "所属网点",
    dataIndex: "branchName",
    scopedSlots: { customRender: "affiliatedBranches" },
    align: "center"
  },
  {
    title: "点位地址",
    dataIndex: "address",
    scopedSlots: { customRender: "poitionSite" },
    align: "center"
  },
  {
    title: "点位状态",
    dataIndex: "status",
    scopedSlots: { customRender: "poitionState" },
    align: "center"
  },
  {
    title: "设备数",
    dataIndex: "deviceNumber",
    scopedSlots: { customRender: "deviceNumber" },
    align: "center"
  },
  {
    title: "合作方数",
    dataIndex: "partnerNum",
    scopedSlots: { customRender: "partnerNumber" },
    align: "center"
  },
  {
    title: "操作",
    dataIndex: "operation",
    scopedSlots: { customRender: "operation" },
    align: "center"
  }
];

const data = [
  {
    key: 1
  }
];

const columnsGetFacility = [
  {
    title: "序号",
    dataIndex: "number",
    scopedSlots: { customRender: "number" },
    align: "center"
  },
  {
    title: "设备编号",
    dataIndex: "devCode",
    scopedSlots: { customRender: "facilityNumer" },
    align: "center"
  }
];

const dataGetFacility = [
  {
    key: 1
  }
];

const columnsPartner = [
  {
    title: "序号",
    dataIndex: "number",
    scopedSlots: { customRender: "number" },
    align: "center"
  },
  {
    title: "合作方名称",
    dataIndex: "name",
    scopedSlots: { customRender: "partnerName" },
    align: "center"
  },
  {
    title: "分润比例",
    dataIndex: "distributionRatio",
    scopedSlots: { customRender: "fenRunProportion" },
    align: "center"
  }
];

const dataPartner = [
  {
    key: 1
  }
];

const columnsPartnerNum = [
  {
    title: "序号",
    dataIndex: "number",
    scopedSlots: { customRender: "number" },
    align: "center"
  },
  {
    title: "合作方名称",
    dataIndex: "name",
    scopedSlots: { customRender: "partnerName" },
    align: "center"
  },
  {
    title: "分润比例",
    dataIndex: "distributionRatio",
    scopedSlots: { customRender: "fenRunProportion" },
    align: "center"
  }
];

const dataPartnerNum = [
  {
    key: 1
  }
];

const columnsFindTeller = [
  {
    title: "寻柜员名称",
    dataIndex: "name",
    scopedSlots: { customRender: "nameTeller" },
    align: "center"
  }
];

const dataFindTeller = [
  {
    key: 1
  }
];

const columnsFindTellerNot = [
  {
    title: "寻柜员名称",
    dataIndex: "name",
    scopedSlots: { customRender: "nameTeller" },
    align: "center"
  }
];

const dataFindTellerNot = [
  {
    key: 1
  }
];

const columnsFacility = [
  {
    title: "编号",
    dataIndex: "devCode",
    scopedSlots: { customRender: "Number" },
    align: "center"
  }
];

const dataFacility = [
  {
    key: 1
  }
];

const columnsFacilityNot = [
  {
    title: "编号",
    dataIndex: "devCode",
    scopedSlots: { customRender: "Number" },
    align: "center"
  }
];

const dataFacilityNot = [
  {
    key: 1
  }
];

const columnsAddPartner = [
  {
    title: "合作方名称",
    dataIndex: "name",
    scopedSlots: { customRender: "partnerName" },
    align: "center"
  },
  // {
  //   title: "分润比例",
  //   dataIndex: "distributionRatio",
  //   scopedSlots: { customRender: "fenRunProportion" },
  //   align: "center"
  // }
];

const dataAddPartner = [
  {
    key: 1
  }
];

export default {
  name: "InspectorAdmin",
  data() {
    return {
      form: this.$form.createForm(this),
      data: [],
      columns,
      pagination: {
        showSizeChanger: true,
        showQuickJumper: true,
        pageSizeOptions: ["10", "20", "30", "40"], //每页条数选择器
        showTotal: (total, range) => {
          return `${range[0]}-${range[1]} 共${total}条`;
        }, // 总数展示
        pageSize: 10, //每页条数
        defaultCurrent: 1, //默认当前页
        current: 1, //当前页
        total: 50 //数据总条数
      },
      visible: false,
      columnsGetFacility,
      dataGetFacility: [],
      getFacilityVisible: false,
      columnsPartner,
      dataPartner: [],
      locale: {
        emptyText: '未绑定合作方',
      },
      partnerVisible: false,
      columnsPartnerNum,
      dataPartnerNum: [],
      partnerNumVisible: false,
      columnsFindTeller,
      dataFindTeller: [],
      findTellerVisible: false,
      isBinding: "解绑",
      columnsFindTellerNot,
      dataFindTellerNot: [],
      facilityVisible: false,
      columnsFacility,
      dataFacility: [],
      columnsFacilityNot,
      dataFacilityNot: [],
      detailsVisible: false,
      posName: "",
      areaInfo: "",
      scene: "",
      population: "",
      sexStructure: "",
      ageStructure: "",
      competitorDevNumbers: "",
      description: "",
      lightTime: "",
      branchName: [],
      addPartnerVisible: false,
      columnsAddPartner,
      dataAddPartner: [],
      partnerNumTitle: '',
      isEditProportion: true,
      isDeletePartner: true,
      selectedKeysAddPartner: [],
      selectedKeysFindTeller: [],
      selectedKeysFindTellerNot: [],
      selectedKeysFacility: [],
      selectedKeysFacilityNot: [],
      isInput: false,
      isFinish: false,
      isCancel: false,
      isAddPartner: true,
      isDelCancel: false,
      isDelOk: false,
      partnerIds: [],
      bindPosDtos: [],
      selectedKeysPartner: [],
    };
  },
  watch: {

  },
  computed: {},
  methods: {
    handleReset() {
      this.form.resetFields();
    },
    // 设置分润比例
    setProportion() {
      this.isInput = true;
      this.isEditProportion = false;
      this.isAddPartner = false;
      this.isDeletePartner = false;
      this.isFinish = true;
      this.isCancel = true;
    },
    // 取消设置标准库存
    setCancel() {
      this.isInput = false;
      this.isFinish = false;
      this.isCancel = false;
      this.isEditProportion = true;
      this.isAddPartner = true;
      this.isDeletePartner = true;
    },
    // 点击删除合作方按钮
    delPartner() {
      this.isEditProportion = false;
      this.isAddPartner = false;
      this.isDeletePartner = false;
      this.isDelCancel = true;
      this.isDelOk = true;
    },
    DelCancel() {
      this.isEditProportion = true;
      this.isAddPartner = true;
      this.isDeletePartner = true;
      this.isDelCancel = false;
      this.isDelOk = false;
    },
    inputNumberChange(value, key) {
      const newData = [...this.dataPartner];
      const target = newData.filter(item => key === item.id)[0];
      if (target) {
        target["distributionRatio"] = String(value);
        this.dataPartner = newData;
      }
    },
    onSelectChangeAddPartner(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedKeysAddPartner = selectedRowKeys;
    },
    onSelectAddPartner(record, selected, selectedRows) {
      console.log(record, selected, selectedRows);
    },
    onSelectChangeFindTeller(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedKeysFindTeller = selectedRowKeys;
    },
    onSelectChangeFindTellerNot(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedKeysFindTellerNot = selectedRowKeys;
    },
    onSelectChangeFacility(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedKeysFacility = selectedRowKeys;
    },
    onSelectChangeFacilityNot(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedKeysFacilityNot = selectedRowKeys;
    },
    onSelectChangePartner(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedKeysPartner = selectedRowKeys;
    },
    tbsChange(checked) {
      if (checked == 1) {
        this.isBinding = "解绑";
        this.deviceBound(this.deviceBoundId);
      }
      if (checked == 2) {
        this.isBinding = "绑定";
        this.deviceUnbind();
      }
    },
    findTellerChange(checked) {
      if (checked == 1) {
        this.isBinding = "解绑";
        this.InspectorBound(this.InspectorBoundId);
      }
      if (checked == 2) {
        this.isBinding = "绑定";
        this.InspectorUnbind();
      }
    },
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination };
      pager.current = pagination.current;
      pager.pageSize = pagination.pageSize;
      pager.total = pagination.total;
      this.pagination = pager;
      this.handleReset();
      this.posList();
    },

    partnerNumModal(text, record) {
      this.partnerNumVisible = true;
      this.partnerId = record.id;
      this.partnerNumberBound(this.partnerId);
      this.partnerNumTitle = record.posName + '-合作方'
    },
    partnerNumOk(e) {
      // this.add();
      this.partnerNumVisible = false;
    },
    partnerNumCancel(e) {
      console.log("取消");
      this.partnerNumVisible = false;
    },

    getFacilityModal(text, record) {
      this.getFacilityVisible = true;
      this.deviceBoundId = record.id;
      this.deviceBound(this.deviceBoundId);
    },
    getFacilityOk(e) {
      this.getFacilityVisible = false;
    },
    getFacilityCancel(e) {
      console.log("取消");
      this.getFacilityVisible = false;
    },
    partnerModal(text, record) {
      this.partnerVisible = true;
      if (record.partnerNum == 0) {
        this.isEditProportion = false;
        this.isDeletePartner = false;
      } else {
        this.isEditProportion = true;
        this.isDeletePartner = true;
      }
      this.partnerUnbindId = record.id;
      this.partnerBound(this.partnerUnbindId);
    },
    partnerOk(e) {
      this.partnerVisible = false;
    },
    partnerCancel(e) {
      this.partnerVisible = false;
    },

    addPartnerModal() {
      this.addPartnerVisible = true;
      this.partnerUnbind();
    },
    addPartnerOk(e) {
      this.addPartnerVisible = false;
      this.partnerbindPar();
    },
    addPartnerCancel(e) {
      this.addPartnerVisible = false;
    },

    findTellerModal(text, record) {
      this.findTellerVisible = true;
      this.InspectorBoundId = record.id;
      this.InspectorBound(this.InspectorBoundId);
    },
    findTellerOk(e) {
      this.findTellerVisible = false;
      this.bindEmp(this.InspectorBoundId);
    },
    findoutOk(e) {
      this.findTellerVisible = false;
      this.unbindEmp(this.InspectorBoundId)
    },
    // PointBindingOk(e) {
    //   this.PointBindingvisible = false;
    //   this.bindPos(this.pointFindBindId);
    // },
    // PointOutOk(e) {
    //   this.PointBindingvisible = false;
    //   this.unbindPos(this.pointFindBindId);
    // },
    findTellerCancel(e) {
      console.log("取消");
      this.findTellerVisible = false;
    },

    facilityModal(text, record) {
      this.facilityVisible = true;
      this.deviceBoundId = record.id;
      this.deviceBound(this.deviceBoundId);
    },
    facilityOk(e) {
      this.facilityVisible = false;
      this.bindDev(this.deviceBoundId);
    },
    facilityoutOk(e) {
      this.facilityVisible = false;
      this.unbindDev(this.deviceBoundId);
    },
    facilityCancel(e) {
      this.facilityVisible = false;
    },

    detailsModal(text, record) {
      this.detailsVisible = true;
      this.pointDetailId = record.id;
      this.pointDetail(this.pointDetailId);
    },
    detailsOk(e) {
      this.detailsVisible = false;
    },
    detailsCancel(e) {
      this.detailsVisible = false;
    },

    showDeleteConfirm(text, record) {
      var that = this;
      this.delId = record.id;
      this.$confirm({
        title: "删除点位",
        content: "您确定要删除这个点位吗？",
        okText: "确定",
        cancelText: "取消",
        iconType: "close-circle",
        onOk() {
          that.delete(that.delId);
        },
        onCancel() {
        }
      });
    },

    brightConfirm(text, record) {
      var that = this;
      this.brightId = record.id;
      this.$confirm({
        title: "确认点亮",
        content: "确定该点位已通电点亮吗？",
        okText: "确定",
        cancelText: "取消",
        iconType: "question-circle",
        onOk() {
          that.light(that.brightId);
        },
        onCancel() {
        }
      });
    },

    // 点位初始化
    listInit() {
      this.axios
        .post(this.baseUrl + "/pos/listInit", {
          loginToken: this.$store.state.loginToken,
        })
        .then(response => {
          // console.log(response);
          this.branchName = response.data.data.branchName;
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 查询点位列表
    posList() {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/pos/posList", {
          loginToken: this.$store.state.loginToken,
          pageNum: this.pagination.current,
          pageSize: this.pagination.pageSize,
          branchName: values.affiliatedBranches,
          posName: values.poitionName,
          status: values.poitionState
        })
        .then(response => {
          this.data = response.data.data.list;
          this.pagination.total = response.data.data.total;
          this.pagination.pageSize = response.data.data.pageSize;
          this.pagination.current = response.data.data.pageNum;
        })
    },

    // 根据点位id查询巡柜员的信息
    InspectorBound(posId) {
      this.axios
        .post(this.baseUrl + "/posEmployee/getBind", {
          loginToken: this.$store.state.loginToken,
          posId: posId
        })
        .then(response => {
          console.log(response);
          this.dataFindTeller = response.data.data;
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 点位-巡柜员查询未被绑定
    InspectorUnbind() {
      this.axios
        .post(this.baseUrl + "/posEmployee/getUnbind", {
          loginToken: this.$store.state.loginToken,
          posId: this.InspectorBoundId
        })
        .then(response => {
          console.log(response);
          this.dataFindTellerNot = response.data.data;
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 绑定巡柜员
    bindEmp(posId) {
      this.axios
        .post(this.baseUrl + "/pos/bindEmp", {
          loginToken: this.$store.state.loginToken,
          posId: posId,
          employeeIds: this.selectedKeysFindTellerNot
        })
        .then(response => {
          // console.log(response);
          if (response.data.code == 0) {
            this.$message.info("绑定巡柜员成功");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 绑定设备
    bindDev(posId) {
      this.axios
        .post(this.baseUrl + "/pos/bindDev", {
          loginToken: this.$store.state.loginToken,
          posId: posId,
          devIds: this.selectedKeysFacilityNot
        })
        .then(response => {
          // console.log(response);
          if (response.data.code == 0) {
            this.$message.info("绑定设备成功");
            this.posList();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 绑定合作方
    partnerbindPar() {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/pos/bindPar", {
          loginToken: this.$store.state.loginToken,
          posId: this.partnerUnbindId,
          partnerIds: this.selectedKeysAddPartner
        })
        .then(response => {
          console.log(response);
          if (response.data.code == 0) {
            this.$message.info("绑定合作方成功");
            this.partnerBound(this.partnerUnbindId);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 解绑巡柜员
    unbindEmp(posId) {
      var param = {
        loginToken: this.$store.state.loginToken,
        posId: posId,
        employeeIds: this.selectedKeysFindTeller
      };
      this.axios
        .delete(this.baseUrl + "/pos/unbindEmp", { data: param })
        .then(response => {
          // console.log(response);
          if (response.data.code == 0) {
            this.$message.info("解绑巡柜员成功");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 解绑设备
    unbindDev(posId) {
      var param = {
        loginToken: this.$store.state.loginToken,
        posId: posId,
        devIds: this.selectedKeysFacility
      };
      this.axios
        .delete(this.baseUrl + "/pos/unbindDev", { data: param })
        .then(response => {
          // console.log(response);
          if (response.data.code == 0) {
            this.$message.info("解绑设备成功");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 解绑合作方
    unbindPar() {
      this.isDelCancel = false;
      this.isDelOk = false;
      this.isEditProportion = true;
      this.isAddPartner = true;
      this.isDeletePartner = true;
      var param = {
        loginToken: this.$store.state.loginToken,
        posId: this.partnerUnbindId,
        partnerIds: this.selectedKeysPartner
      };
      this.axios
        .delete(this.baseUrl + "/pos/unbindPar", { data: param })
        .then(response => {
          // console.log(response);
          if (response.data.code == 0) {
            this.$message.info("删除合作方成功");
            this.partnerBound(this.partnerUnbindId);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 查询被绑定的设备
    deviceBound(posId) {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/posDevice/getBind", {
          loginToken: this.$store.state.loginToken,
          devCode: values.facilityNumber,
          posId: posId
        })
        .then(response => {
          console.log(response);
          this.dataFacility = response.data.data;
          this.dataGetFacility = response.data.data;
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 查询未被绑定的设备
    deviceUnbind() {
      this.axios
        .post(this.baseUrl + "/posDevice/getUnbind", {
          loginToken: this.$store.state.loginToken
        })
        .then(response => {
          // console.log(response);
          this.dataFacilityNot = response.data.data;
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 根据点位id查询合作方的信息
    partnerNumberBound(posId) {
      this.axios
        .post(this.baseUrl + "/posPartner/getBind", {
          loginToken: this.$store.state.loginToken,
          posId: posId
        })
        .then(response => {
          // console.log(response);
          this.dataPartnerNum = response.data.data;
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 根据点位id查询合作方的信息
    partnerBound(posId) {
      this.axios
        .post(this.baseUrl + "/posPartner/getBind", {
          loginToken: this.$store.state.loginToken,
          posId: posId
        })
        .then(response => {
          // console.log(response);
          this.dataPartnerNum = response.data.data;
          this.dataPartner = response.data.data;
          this.dataPartner.map((item, index, arr) => {
            this.partnerIds.push(item.id)
          })
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 修改合作方分润比率
    posUpdate() {
      this.isInput = false;
      this.isFinish = false;
      this.isCancel = false;
      this.isEditProportion = true;
      this.isAddPartner = true;
      this.isDeletePartner = true;
      this.axios
        .put(this.baseUrl + "/pos/update", {
          loginToken: this.$store.state.loginToken,
          posId: this.partnerUnbindId,
          updateDtos: this.dataPartner
        })
        .then(response => {
          // console.log(response);
          if (response.data.code == 0) {
            this.$message.info("修改分润比率成功");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 点位-合作方查询未被绑定
    partnerUnbind() {
      var values = this.form.getFieldsValue();
      this.axios
        .post(this.baseUrl + "/posPartner/getUnbind", {
          loginToken: this.$store.state.loginToken,
          name: values.partnerName,
          partnerIds: this.partnerIds
        })
        .then(response => {
          // console.log(response);
          this.dataAddPartner = response.data.data;
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 点位详情
    pointDetail(posId) {
      this.axios
        .post(this.baseUrl + "/pos/detail", {
          loginToken: this.$store.state.loginToken,
          posId: posId
        })
        .then(response => {
          console.log(response);
          this.posName = response.data.data.posName;
          this.areaInfo = response.data.data.areaInfo;
          this.scene = response.data.data.scene;
          this.population = response.data.data.population;
          this.sexStructure = response.data.data.sexStructure;
          this.ageStructure = response.data.data.ageStructure;
          this.competitorDevNumbers = response.data.data.competitorDevNumbers;
          this.description = response.data.data.description;
          this.lightTime = response.data.data.lightTime;
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 编辑点位--传id
    editShowPos(text, record) {
      console.log(record);
      var editId = record.id;
      this.$store.commit("editId", editId);
      this.$router.push({
        path: "/home/networkadmin/poitionadmin/poitionedit"
      });
    },

    // 删除点位
    delete(posId) {
      var param = { loginToken: this.$store.state.loginToken, posId: posId };
      this.axios
        .delete(this.baseUrl + "/pos/delete", { data: param })
        .then(response => {
          console.log(response);
          if (response.data.code == 0) {
            this.$message.info("删除点位成功");
            this.posList();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 点亮点位
    light(posId) {
      this.axios
        .put(this.baseUrl + "/pos/light", {
          loginToken: this.$store.state.loginToken,
          posId: posId
        })
        .then(response => {
          console.log(response);
          if (response.data.code == 0) {
            this.posList();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 导出点位
    listExport() {
      var values = this.form.getFieldsValue();
      this.axios
        .post(
          this.baseUrl + "/pos/export",
          {
            loginToken: this.$store.state.loginToken,
            branchName: values.affiliatedBranches,
            posName: values.poitionName,
            status: values.poitionState
          },
          { responseType: "arraybuffer" }
        )
        .then(response => {
          console.log(response);
          if (response.status == "200") {
            const aLink = document.createElement("a");
            let blob = new Blob([response.data], {
              type: "application/vnd.ms-excel"
            });
            aLink.href = URL.createObjectURL(blob);
            aLink.download = "点位管理表";
            aLink.click();
            document.body.appendChild(aLink);
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  },
  created() {
    this.listInit();
    this.posList();
  },
  mounted() { }
};